package com.tuishan.myapplication.Custom;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/19
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class BuyDialog extends Dialog implements View.OnClickListener {
    Button Ok_btn, Cancel_btn;
    EditText pay_password, pay_remarks;
    LoadingView Loading;
    int ProId;

    public BuyDialog(Context context, LoadingView arg1, int arg2) {
        super(context, R.style.dialogUser);
        setContentView(R.layout.buy_trans_dialog);
        this.Loading = arg1;
        this.ProId = arg2;
        Ok_btn = findViewById(R.id.Ok_btn);
        Cancel_btn = findViewById(R.id.Cancel_btn);
        pay_password = findViewById(R.id.pay_password);
        pay_remarks = findViewById(R.id.pay_remarks);
        Ok_btn.setOnClickListener(this);
        Cancel_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Cancel_btn:
                this.dismiss();
                break;
            case R.id.Ok_btn:
                if (StringUtils.isEmpty(pay_password.getText().toString().trim())) {
                    ToastUtils.showLong("请输入资金密码");
                    return;
                }
                BuyTrans(ProId, pay_password.getText().toString().trim(),
                        pay_remarks.getText().toString().trim());
                break;
        }
    }

    //购买
    private void BuyTrans(int productId, String moneyPwd, String remarks) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("productId", productId);
        objectMap.put("moneyPwd", moneyPwd);
        objectMap.put("remarks", remarks);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.BuyCollectionTransactUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                ToastUtils.showShort("收藏成功");
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

}
